package com.cf.game.im.handler.relation;

import com.cf.constant.EnumReturnCode;
import com.cf.game.group.RelationService;
import com.cf.game.group.vo.RelationVo;
import com.cf.game.im.handler.Handler;
import com.cf.game.msg.FriendRequest;
import com.cf.game.msg.FriendResponse;
import com.cf.game.msg.Response;
import com.cf.game.node.NodeService;
import com.cf.mapper.BeanMapper;
import com.cf.util.ObjectJsonUtil;
import io.vertx.core.eventbus.EventBus;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.List;
import java.util.Map;

/**
 * boot-game-FriendHandler
 *
 * @author <a href="2428922347@qq.com">Zhu mingming</a>
 * @info
 * @since 2018/8/1 下午 12:29
 */
@Slf4j
@Component
public class FriendHandler implements Handler {

    @Autowired
    private BeanMapper beanMapper;

    @Autowired
    private NodeService nodeService;

    @Autowired
    private RelationService relationService;

    @Override
    public void handle(Map dataMap, EventBus eventBus) {
        FriendRequest fr = beanMapper.map(dataMap, FriendRequest.class);
        friendAction(fr, eventBus);
    }


    public void friendAction(FriendRequest fr, EventBus ebs) {
        List<RelationVo> relationVos = relationService.myRelation(fr.getSender());
        String sendNode = nodeService.getUserNode(fr.getSender());
        if (!StringUtils.isEmpty(sendNode)) {
            FriendResponse friendResponse = new FriendResponse(relationVos);
            ebs.publish(sendNode, ObjectJsonUtil.toString(Response.instance(fr.getSender(), EnumReturnCode.SUCCESS.getCode(), ObjectJsonUtil.toString(friendResponse))));
        }
    }

    @Override
    public String getSupport() {
        return "friend";
    }
}
